package lanbo.cj.lmp.activity;

import android.content.SharedPreferences;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import lanbo.cj.lmp.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.bmob.im.BmobUserManager;
import cn.bmob.im.bean.BmobChatUser;
import cn.bmob.im.util.BmobLog;
import cn.bmob.v3.listener.SaveListener;
import lanbo.cj.lmp.bean.KeShi;
import lanbo.cj.lmp.bean.User;

import java.util.List;

public class LoginActivity extends Activity {

	EditText et_username, et_password;
	Button btn_login;
	BmobChatUser currentUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		init();

	}

	private void init() {
		et_username = (EditText) findViewById(R.id.et_username);
		et_password = (EditText) findViewById(R.id.et_password);
		et_username.setTextColor(0XFF000000);
		et_password.setTextColor(0XFF000000);
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				login();
			}
		});
	}

	private void login() {
		String name = et_username.getText().toString();
		String password = et_password.getText().toString();

		if (TextUtils.isEmpty(name)) {
			Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
			return;
		}

		if (TextUtils.isEmpty(password)) {
			Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
			return;
		}

		final ProgressDialog progress = new ProgressDialog(LoginActivity.this);
		progress.setMessage("正在登陆...");
		progress.setCanceledOnTouchOutside(false);
		progress.show();
		BmobUserManager userManager = BmobUserManager.getInstance(LoginActivity.this);
		userManager.login(name, password, new SaveListener() {

			@Override
			public void onSuccess() {
				runOnUiThread(new Runnable() {
					public void run() {
						progress.setMessage("正在获取好友列表...");
					}
				});
				progress.dismiss();
				setUserKeShiName();
				Intent intent = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}

			@Override
			public void onFailure(int errorcode, String arg0) {
				// TODO Auto-generated method stub
				progress.dismiss();
				BmobLog.i(arg0);
				Toast.makeText(LoginActivity.this, arg0, Toast.LENGTH_SHORT).show();
			}
		});

	}
	private void setUserKeShiName(){
		SharedPreferences setPre = this.getSharedPreferences("user", 0);
		final SharedPreferences.Editor keshiEditor = setPre.edit();
		BmobQuery<KeShi> query = new BmobQuery<KeShi>();
		query.addWhereEqualTo("keshiId", BmobUser.getCurrentUser(this, User.class).getKeshiId());
		query.findObjects(this, new FindListener<KeShi>() {
			@Override
			public void onSuccess(List<KeShi> list) {
				String keshiName = list.get(0).getKeshiName();
				keshiEditor.putString("keshiName", keshiName);
				keshiEditor.commit();
			}
			@Override
			public void onError(int i, String s) {

			}
		});
	}

}
